package com.lcl.jarvis.common.utils;

import com.lcl.jarvis.common.model.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

/**
 * @author licl
 * @desc 获取用户授权信息
 * @date 2023/04/10 12:21
 **/
@Slf4j
public class AuthUtils {

    /**
     * 获取登录用户
     *
     * @return
     */
    public static SysUser getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return Optional.ofNullable(authentication).map(item -> {
            Object principal = item.getPrincipal();
            if (principal instanceof SysUser) {
                return (SysUser) principal;
            }
            return null;
        }).orElse(null);
    }

    /**
     * 获取登陆的用户id
     *
     * @return
     */
    public static Long getUserId() {
        SysUser currentUser = getCurrentUser();
        return Optional.ofNullable(currentUser).map(SysUser::getId).orElse(null);
    }

    /**
     * 获取登陆的用户名
     *
     * @return
     */
    public static String getUsername() {
        SysUser currentUser = getCurrentUser();
        return Optional.ofNullable(currentUser).map(SysUser::getUsername).orElse("");
    }

}
